package com.beyond.pm.api

import com.beyond.common.err.UnLoginException
import com.beyond.common.util.LogUtils
import com.beyond.common.util.ResourceAppUtils
import com.beyond.pm.R
import com.jung.net.BaseResponse

object MyApiResult {
    @Throws(Exception::class)
    inline fun <T> create(request: () -> BaseResponse<T>): T? {
        val response: BaseResponse<T>?
        try {
            response = request.invoke()
        } catch (e: Exception) {
            LogUtils.e(e.message)
            throw Exception(ResourceAppUtils.getString(R.string.basic_net_error))
        }
        when (response.code) {
            "200" -> {
                return response.data
            }
            "401" -> {
                // 没有登录
                throw UnLoginException()
            }
            else -> {
                throw Exception(response.msg)
            }
        }
    }
}